﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telerik.Windows.Controls;
using PersonalHealth.Data.Model;
using Telerik.Windows.Controls.Charting;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;

namespace PersonHealth
{
    public class ChartFill
    {
        public static void GenerateBloodPressure(RadChart rc, IList<Measure_BP> list)
        {
            list = list.OrderBy(c => c.MeasureTime).ToList();

            #region chart

            rc.SeriesMappings.Clear();
            rc.ItemsSource = null;
            rc.DefaultView.ChartArea.EnableAnimations = true;
            rc.DefaultView.ChartLegend.Header = "血压";

            //rc.DefaultView.ChartTitle.BorderBrush = Application.Current.FindResource("Transparent") as Brush;
            //rc.DefaultView.ChartTitle.OuterBorderBrush = Application.Current.FindResource("Transparent") as Brush;
            //rc.DefaultView.ChartTitle.Background = Application.Current.FindResource("Transparent") as Brush;
            //(rc.DefaultView.ChartTitle.Content as TextBlock).Text = "2011年2月血压";

            //rc.DefaultView.ChartLegend.Background = Application.Current.FindResource("Transparent") as Brush;
            //rc.DefaultView.ChartLegend.BorderBrush = Application.Current.FindResource("Transparent") as Brush;
            //rc.DefaultView.ChartLegend.LegendItemMarkerShape = MarkerShape.Square;
            //rc.DefaultView.ChartLegend.Width = 160;
            //rc.DefaultView.ChartLegend.Margin = new Thickness(-10, -20, 10, 0);

            SeriesMapping mapping = new SeriesMapping();
            mapping.LegendLabel = "收缩压";
            mapping.CollectionIndex = 0;
            SplineSeriesDefinition def = new SplineSeriesDefinition();
            def.ShowItemToolTips = true;
            def.ItemLabelFormat = "0.#";
            def.ShowItemLabels = true;
            def.Appearance = new SeriesAppearanceSettings { Fill = Application.Current.FindResource("actual") as Brush };
            mapping.SeriesDefinition = def;
            mapping.ItemMappings.Add(new ItemMapping("Value", DataPointMember.YValue));
            mapping.ItemMappings.Add(new ItemMapping("Tooltip", DataPointMember.Tooltip){ FieldType = typeof(int)});
            rc.SeriesMappings.Add(mapping);

            mapping = new SeriesMapping();
            mapping.LegendLabel = "舒张压";
            mapping.CollectionIndex = 1;

            def = new SplineSeriesDefinition();
            def.ShowItemToolTips = true;
            def.ItemLabelFormat = "0.#";
            def.ShowItemLabels = true;
            def.Appearance = new SeriesAppearanceSettings { Fill = Application.Current.FindResource("actual") as Brush };
            mapping.SeriesDefinition = def;
            mapping.ItemMappings.Add(new ItemMapping("Value", DataPointMember.YValue));
            mapping.ItemMappings.Add(new ItemMapping("Tooltip", DataPointMember.Tooltip));
            rc.SeriesMappings.Add(mapping);

            mapping = new SeriesMapping();
            mapping.LegendLabel = "脉率";
            mapping.CollectionIndex = 2;
            def = new SplineSeriesDefinition();
            def.ShowItemToolTips = true;
            def.ItemLabelFormat = "0.#";
            def.ShowItemLabels = true;
            def.Appearance = new SeriesAppearanceSettings { Fill = Application.Current.FindResource("actual") as Brush };
            mapping.SeriesDefinition = def;
            mapping.ItemMappings.Add(new ItemMapping("Value", DataPointMember.YValue));
            mapping.ItemMappings.Add(new ItemMapping("Tooltip", DataPointMember.Tooltip));
            rc.SeriesMappings.Add(mapping);

            List<List<ChartValue>> data = new List<List<ChartValue>>();
            data.Add(list.Select(c => new ChartValue { Value = c.SBP, Tooltip = c.MeasureTime.ToString("yy年MM月dd日  hh：mm") }).ToList());
            data.Add(list.Select(c => new ChartValue { Value = c.DBP, Tooltip = c.MeasureTime.ToString("yy年MM月dd日  hh：mm") }).ToList());
            data.Add(list.Select(c => new ChartValue { Value = c.HR_Value, Tooltip = c.MeasureTime.ToString("yy年MM月dd日  hh：mm") }).ToList());


            rc.ItemsSource = data;

            #endregion
        }

        class ChartValue
        {
            public DateTime Time { get; set; }
            public decimal Value { get; set; }
            public string Tooltip { get; set; }
        }
    }
}
